// 最小栈
class MinStack {
    constructor() {
        this.list = []
        this.sortedList = []
    }
    get length() {
        return this.list.length
    }
    push(item) {
        this.list.push(item)
        this._insertItemToSortedList(item)
    }
    pop() {
        if (this.list.length === 0) {
            return undefined
        }
        let item = this.list.pop()
        this._deleteItemFromSortedList(item)
        return item
    }
    top() {
        return this.list[this.length - 1]
    }
    getMin() {
        return this.sortedList[0]
    }
    _insertItemToSortedList(item) {
        if (this.sortedList.length === 0) {
            return this.sortedList.push(item)
        }
        let len = this.sortedList.length
        for (let index = 0; index < len; index++) {
            const cur = this.sortedList[index];
            if (item <= cur) {
                return this.sortedList.splice(index, 0, item)
            }
        }
        return this.sortedList.push(item)
    }
    _deleteItemFromSortedList(item) {
        let index = this.sortedList.findIndex(i => i === item)
        if (index !== -1) {
            this.sortedList.splice(index, 1)
        }
    }
}

class MinStack2 {
    constructor() {
        this.list = []
    }

    get length() {
        return this.list.length
    }

    push(item) {
        this.list.push({
            value: item,
            min: this.length === 0 ? item : Math.min(x, this.getMin())
        })
    }

    pop(){
        return this.list.pop()
    }

    top(){
        return this.list[this.length - 1].value
    }

    getMin() {
        if(this.length === 0){
            return undefined
        }
        return this.list[this.length - 1].min;
    }
}